#include "charttable.h"
#include "ui_charttable.h"
#include "serial.h"
#include <QMessageBox>

ChartTable::ChartTable(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChartTable)
{
    ui->setupUi(this);

    table_init();      //表格初始化

    /*---------------------------表格设计信号与槽(Begin)-------------------------------------*/
    connect(ui->cb_tscroll_auto, SIGNAL(clicked()), this, SLOT(cb_tscroll_auto()));          //自动滚动复选框
    connect(ui->btn_page_up, &QPushButton::clicked, this, &ChartTable::btn_page_up_click);       //上一页
    connect(ui->btn_page_down, &QPushButton::clicked, this, &ChartTable::btn_page_down_click);   //下一页
    connect(ui->btn_page_home, &QPushButton::clicked, this, &ChartTable::btn_page_home_click);   //首页
    connect(ui->btn_page_last, &QPushButton::clicked, this, &ChartTable::btn_page_last_click);   //尾页
    connect(ui->btn_table_clear, &QPushButton::clicked, this, &ChartTable::btn_page_clear_click);//清空表格
    /*---------------------------表格设计信号与槽(End)-------------------------------------*/
}

/*---------------------------表格相关设计函数(Begin)-------------------------------------*/
/**
 * @brief 表格初始化
 */
void ChartTable::table_init()
{
    ui->tableWidget->setRowCount(12);           //设置行数
    ui->tableWidget->setColumnCount(6);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"索引"<<"时间"<<"温度"<<"状态"<<"流量"<<"状态");
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//单元格不可编辑
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(2,20,130);color: white;}");//设置表头背景和字体颜色
    ui->tableWidget->verticalHeader()->setVisible(false);   //隐藏列表头

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);    //第一列索引列宽最小，其他列自适应列宽
//    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);  //自适应行高
    ui->lb_page->setText("/1");
    ui->le_current_page->setText("1");
}

/**
 * @brief 表格定时插入数据
 */
void ChartTable::insert_item(float temper, float flow, Ui::Serial *uiS)
{
    int row_count = ui->tableWidget->rowCount();    //查找当前已经设置的行数

    if((table_row_count-1)%12 == 0 && (table_row_count-1)!=0){  //第13行时需要插入一个新的页面
        ui->tableWidget->setRowCount((row_count)+12); //行数不够，直接添加一个新页面
        page_int = ((table_row_count-1)%12==0)?(page_int+1):(page_int);  //设置页数
        page_current = page_int;    //当前页面
        QString page_str = QString::number(page_int, 10);
        ui->lb_page->setText("/"+page_str);         //显示总页数
        if(t_scroll_bottom_flag){   //滚动条显示在最底部的标志
            ui->tableWidget->scrollToBottom();  //换页时将滚动条设置到最底部达到换页效果
            ui->le_current_page->setText(page_str); //只有滚动在最底部时才将当前页显示为最新页面
        }
    }

    //qDebug()<<"当前表最大行数："<<ui->tableWidget->rowCount()<<"\t 当前插入行count="<<table_row_count<<"\t page:"<<page_int;
    //qDebug()<<"滚动条最大位置:"<<max_scroll<<"\t当前滚动条位置:"<<ui->tableWidget->verticalScrollBar()->value();

    if(page_int == 1){  //第一页
        //插入索引
        ui->tableWidget->setItem(table_row_count-1,0, new QTableWidgetItem(QString::number(table_row_count, 10)));
        //插入时间
        ui->tableWidget->setItem(table_row_count-1,1, new QTableWidgetItem(QDateTime::currentDateTime().toString("hh:mm:ss")));
        //插入温度数据
        if(Serial::serialClass->unit_temper_ssd){
            ui->tableWidget->setItem(table_row_count-1,2, new QTableWidgetItem(QString("%1 ℃").arg(temper)));
        }else{
            ui->tableWidget->setItem(table_row_count-1,2, new QTableWidgetItem(QString("%1 ℉").arg(32+temper*1.8)));
        }

        //if(temper > uiS->le_Upper_temper->text().split("℃").first().toFloat()){
        if(temper > Serial::serialClass->obj_safe_scope.temper_upper){
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("温度过高"));
        //}else if(temper < uiS->le_Lower_temper->text().split("℃").first().toFloat()){
        }else if(temper < Serial::serialClass->obj_safe_scope.temper_lower){
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("温度过低"));
        }else{
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("正常"));
        }

        //插入流量数据
        if(Serial::serialClass->unit_flow_ml){
            ui->tableWidget->setItem(table_row_count-1,4, new QTableWidgetItem(QString("%1 ml/min").arg(flow)));
        }else{
            ui->tableWidget->setItem(table_row_count-1,4, new QTableWidgetItem(QString("%1 L/H").arg(flow*0.06)));
        }

        //if(flow > uiS->le_Upper_flow->text().split("m").first().toFloat()){
        if(flow > Serial::serialClass->obj_safe_scope.flow_upper){
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("流量过高"));
        //}else if(flow < uiS->le_Lower_flow->text().split("m").first().toFloat()){
        }else if(flow < Serial::serialClass->obj_safe_scope.flow_lower){
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("流量过低"));
        }else{
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("正常"));
        }
//        ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("危险"));

        for(int i=0;i<=5;i++){
            ui->tableWidget->item(table_row_count-1,i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }else{  //多页
        ui->tableWidget->setItem(table_row_count-1,0, new QTableWidgetItem(QString::number(table_row_count, 10)));
        ui->tableWidget->setItem(table_row_count-1,1, new QTableWidgetItem(QDateTime::currentDateTime().toString("hh:mm:ss")));
        //插入温度数据
        if(Serial::serialClass->unit_temper_ssd){
            ui->tableWidget->setItem(table_row_count-1,2, new QTableWidgetItem(QString("%1 ℃").arg(temper)));
        }else{
            ui->tableWidget->setItem(table_row_count-1,2, new QTableWidgetItem(QString("%1 ℉").arg(32+temper*1.8)));
        }
        if(temper > Serial::serialClass->obj_safe_scope.temper_upper){
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("温度过高"));
        }else if(temper < Serial::serialClass->obj_safe_scope.temper_lower){
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("温度过低"));
        }else{
            ui->tableWidget->setItem(table_row_count-1,3, new QTableWidgetItem("正常"));
        }

        //插入流量数据
        if(Serial::serialClass->unit_flow_ml){
            ui->tableWidget->setItem(table_row_count-1,4, new QTableWidgetItem(QString("%1 ml/min").arg(flow)));
        }else{
            ui->tableWidget->setItem(table_row_count-1,4, new QTableWidgetItem(QString("%1 L/H").arg(flow*0.06)));
        }
        if(flow > Serial::serialClass->obj_safe_scope.flow_upper){
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("流量过高"));
        }else if(flow < Serial::serialClass->obj_safe_scope.flow_lower){
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("流量过低"));
        }else{
            ui->tableWidget->setItem(table_row_count-1,5, new QTableWidgetItem("正常"));
        }


        for(int i=0;i<=5;i++){
            ui->tableWidget->item(table_row_count-1,i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
    table_row_count ++;
}

/**
 * @brief 表格自动翻页复选框
 */
void ChartTable::cb_tscroll_auto()
{
    if(ui->cb_tscroll_auto->isChecked()){ //自动翻页复选框按下
        t_scroll_bottom_flag = true;
        ui->tableWidget->scrollToBottom();
        QString page_str = QString::number(page_int, 10);
        ui->le_current_page->setText(page_str);
        Serial::serialClass->show_tips("表格自动翻页已打开");
        Serial::serialClass->print_log("表格自动翻页已打开", 0);
    }else{
        t_scroll_bottom_flag = false;
        Serial::serialClass->show_tips("表格自动翻页已关闭");
        Serial::serialClass->print_log("表格自动翻页已关闭", 0);
    }
}

/**
 * @brief 表格上一页按钮
 */
void ChartTable::btn_page_up_click()
{
    if(ui->le_current_page->text().toInt()-1 > 0){
        page_current = page_current-1;    //当前页面
        QString page_str = QString::number(page_current, 10);

        t_scroll_bottom_flag = false;   //false
        ui->tableWidget->verticalScrollBar()->value();
        ui->tableWidget->verticalScrollBar()->setSliderPosition(ui->tableWidget->verticalScrollBar()->value()-12);
        ui->le_current_page->setText(QString::number((ui->le_current_page->text().toInt()-1),10));
        ui->cb_tscroll_auto->setCheckState(Qt::Unchecked);
    }
}
/**
 * @brief 表格下一页按钮
 */
void ChartTable::btn_page_down_click()
{
    if(ui->le_current_page->text().toInt()+1 <= page_int){
        t_scroll_bottom_flag = false;   //false
        ui->tableWidget->verticalScrollBar()->value();
        ui->tableWidget->verticalScrollBar()->setSliderPosition(ui->tableWidget->verticalScrollBar()->value()+12);
        ui->le_current_page->setText(QString::number((ui->le_current_page->text().toInt()+1),10));
        ui->cb_tscroll_auto->setCheckState(Qt::Unchecked);
    }
}

/**
 * @brief 表格首页按钮
 */
void ChartTable::btn_page_home_click()
{
    t_scroll_bottom_flag = false;   //false
    ui->tableWidget->scrollToTop();
    ui->le_current_page->setText("1");
    ui->cb_tscroll_auto->setCheckState(Qt::Unchecked);
}

/**
 * @brief 表格尾页按钮
 */
void ChartTable::btn_page_last_click()
{
    t_scroll_bottom_flag = true;   //true
    ui->tableWidget->scrollToBottom();
    QString page_str = QString::number(page_int, 10);
    ui->le_current_page->setText(page_str);
    ui->cb_tscroll_auto->setCheckState(Qt::Checked);
}

/**
 * @brief 清空表格
 */
void ChartTable::btn_page_clear_click()
{
    QMessageBox *msgbox = new QMessageBox;
    Serial::serialClass->msgboxcreate(msgbox, "提示","确定要清空表格吗？", 1);
    int ret = msgbox->exec();
    if(ret == QMessageBox::Ok){
        //清空表格所作的操作
        ui->tableWidget->clear();
        ui->le_current_page->setText("1");
        ui->lb_page->setText("/1");
        t_scroll_bottom_flag = true;
        table_row_count = 1;
        page_int = 1;
        page_current = 1;
        table_init();
        Serial::serialClass->show_tips("表格数据以清空");
        Serial::serialClass->print_log("表格数据以清空", 0);
    }
}
/*---------------------------表格相关设计函数(End)-------------------------------------*/


ChartTable::~ChartTable()
{
    delete ui;
}
